package com.orangelife.service.propFeeComputeRuleInfo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.PropFeeType.IPropFeeTypeService;
import com.orangelife.service.PropFeeType.model.PropFeeTypeDto;
import com.orangelife.service.feeDetail.IFeeDetailService;
import com.orangelife.service.paybill.IPayBillService;
import com.orangelife.service.propFeeCommInfo.IPropFeeCommInfoService;
import com.orangelife.service.propFeeComputeRule.IPropFeeComputeRuleService;
import com.orangelife.service.propFeeComputeRule.model.PropFeeComputeRuleDto;
import com.orangelife.service.propFeeComputeRuleInfo.IPropFeeComputeRuleInfoService;
import com.orangelife.service.propFeeRuleLinkApartment.IPropFeeRuleLinkApartmentService;
import com.orangelife.service.propFeeRuleLinkApartment.model.PropFeeRuleLinkApartmentDto;
import com.orangelife.service.propFeeRuleLinkMeter.model.PropFeeRuleLinkMeterDto;
import com.orangelife.service.propFeeRuleParam.model.PropFeeRuleParamDto;
import com.orangelife.service.propinfo.IPropInfoService;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.service.propFeeComputeRule.entity.PropFeeComputeRule;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeComputeRuleInfo类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-08-24 18:02:53
 * ==============================================================
 */
@Service("propFeeComputeRuleInfoService")
public class PropFeeComputeRuleInfoServiceImpl extends
		MybaitsGenericDaoSubLogic implements IPropFeeComputeRuleInfoService {

	private static Logger logger = Logger
			.getLogger(PropFeeComputeRuleInfoServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	@Resource(name = "feeDetailService")
	private IFeeDetailService feeDetailService;
	@Resource(name = "propInfoService")
	private IPropInfoService propInfoService;

	@Resource(name = "propFeeTypeService")
	private IPropFeeTypeService propFeeTypeService;

	@Resource(name = "propFeeRuleLinkApartmentService")
	private IPropFeeRuleLinkApartmentService propFeeRuleLinkApartmentService;

	@Resource(name = "payBillService")
	private IPayBillService payBillService;

	@Resource(name = "propFeeCommInfoService")
	private IPropFeeCommInfoService propFeeCommInfoService;

	@Resource(name = "propFeeComputeRuleService")
	private IPropFeeComputeRuleService propFeeComputeRuleService;

	public static void setLogger(Logger logger) {
		PropFeeComputeRuleInfoServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeComputeRuleInfo(
			PropFeeComputeRuleDto propFeeComputeRuleInfo)
			throws AppBizException {

		Assert.notNull(propFeeComputeRuleInfo);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRuleName())) {
			if (propFeeComputeRuleInfo.getRuleName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRuleType())) {
			if (propFeeComputeRuleInfo.getRuleType().length() > 20) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则类型长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRule())) {
			if (propFeeComputeRuleInfo.getRule().length() > 3000) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRoundType())) {
			if (propFeeComputeRuleInfo.getRoundType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"进位类型 1四舍五入（默认）2舍去长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getPeriodType())) {
			if (propFeeComputeRuleInfo.getPeriodType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"生成周期(1按月，2按季)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getCurDate())) {
			if (propFeeComputeRuleInfo.getCurDate().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "当前生成日期长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getStatus())) {
			if (propFeeComputeRuleInfo.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeComputeRuleInfo.getValidityType())) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					"有效期类型（1永久有效2时间段内有效,3一次性）不能为空");
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getValidityType())) {
			if (propFeeComputeRuleInfo.getValidityType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"有效期类型（1永久有效2时间段内有效,3一次性）长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getStartTime())) {
			if (propFeeComputeRuleInfo.getStartTime().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "临时开始时间长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getEndTime())) {
			if (propFeeComputeRuleInfo.getEndTime().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "临时结束时间长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getCompRule())) {
			if (propFeeComputeRuleInfo.getCompRule().length() > 1024) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "完整公式长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRuleConfig())) {
			if (propFeeComputeRuleInfo.getRuleConfig().length() > 1024) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "配置说明长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getCreateuser())) {
			if (propFeeComputeRuleInfo.getCreateuser().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		propFeeComputeRuleInfo.setRuleID(IdUtil.getuuId());
		propFeeComputeRuleInfo.setItemStat(TypeConst.FLAG_ON);
		propFeeComputeRuleInfo.setTscreate(new Date());
		propFeeComputeRuleInfo.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeComputeRuleInfoMapper.insertSelective",
				propFeeComputeRuleInfo);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeComputeRuleInfo(
			PropFeeComputeRuleDto propFeeComputeRuleInfo)
			throws AppBizException {

		Assert.notNull(propFeeComputeRuleInfo);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeComputeRuleInfo.getRuleID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeComputeRuleDto oldPropFeeComputeRuleInfo = this
				.findPropFeeComputeRuleInfoByID(propFeeComputeRuleInfo
						.getRuleID());

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRuleName())) {
			if (propFeeComputeRuleInfo.getRuleName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRuleType())) {
			if (propFeeComputeRuleInfo.getRuleType().length() > 20) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则类型长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRule())) {
			if (propFeeComputeRuleInfo.getRule().length() > 3000) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "规则长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRoundType())) {
			if (propFeeComputeRuleInfo.getRoundType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"进位类型 1四舍五入（默认）2舍去长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getPeriodType())) {
			if (propFeeComputeRuleInfo.getPeriodType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"生成周期(1按月，2按季)长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getCurDate())) {
			if (propFeeComputeRuleInfo.getCurDate().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "当前生成日期长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getStatus())) {
			if (propFeeComputeRuleInfo.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeComputeRuleInfo.getValidityType())) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					"有效期类型（1永久有效2时间段内有效,3一次性）不能为空");
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getValidityType())) {
			if (propFeeComputeRuleInfo.getValidityType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"有效期类型（1永久有效2时间段内有效,3一次性）长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getStartTime())) {
			if (propFeeComputeRuleInfo.getStartTime().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "临时开始时间长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getEndTime())) {
			if (propFeeComputeRuleInfo.getEndTime().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "临时结束时间长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getCompRule())) {
			if (propFeeComputeRuleInfo.getCompRule().length() > 1024) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "完整公式长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getRuleConfig())) {
			if (propFeeComputeRuleInfo.getRuleConfig().length() > 1024) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "配置说明长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeComputeRuleInfo.getCreateuser())) {
			if (propFeeComputeRuleInfo.getCreateuser().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (oldPropFeeComputeRuleInfo == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeComputeRuleInfo.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeComputeRuleInfoMapper.updateByPrimaryKeySelective",
				propFeeComputeRuleInfo);
	}

	@Override
	public PropFeeComputeRuleDto findPropFeeComputeRuleInfoByID(
			String propFeeComputeRuleInfoID) throws AppBizException {
		if (StringUtils.isBlank(propFeeComputeRuleInfoID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeComputeRuleDto oldPropFeeComputeRuleInfo = this
				.getSqlSession()
				.selectOne(
						"PropFeeComputeRuleInfoSearchMapper.selectByPrimaryKey",
						propFeeComputeRuleInfoID);
		if (oldPropFeeComputeRuleInfo == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeComputeRuleInfo.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeComputeRuleInfo;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeComputeRuleInfoByLogic(
			String propFeeComputeRuleInfoID) throws AppBizException {

		if (StringUtils.isBlank(propFeeComputeRuleInfoID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeComputeRuleDto oldPropFeeComputeRuleInfo = this
				.findPropFeeComputeRuleInfoByID(propFeeComputeRuleInfoID);

		oldPropFeeComputeRuleInfo.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeComputeRuleInfo.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeComputeRuleInfoMapper.updateByPrimaryKeySelective",
				oldPropFeeComputeRuleInfo);
	}

	@Override
	public List<Map<String, Object>> findPropFeeComputeRuleInfoByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleInfoSearchMapper.findAllPropFeeComputeRuleInfo",
						map);
		return list;
	}

	@Override
	public List<PropFeeComputeRuleDto> getPropFeeComputeRuleInfoByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeComputeRuleDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleInfoSearchMapper.getAllPropFeeComputeRuleInfoDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeComputeRuleInfoByRelevantID(
			String ruleID, String commID, String feeTypeID, String ruleType,
			String roundType, String periodType, String status,
			String validityType) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", ruleID);
		map.put("commID", commID);
		map.put("feeTypeID", feeTypeID);
		map.put("ruleType", ruleType);
		map.put("roundType", roundType);
		map.put("periodType", periodType);
		map.put("status", status);
		map.put("validityType", validityType);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleInfoSearchMapper.selectPropFeeComputeRuleInfoDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeComputeRuleDto> findPropFeeComputeRuleInfoByRelevantID(
			String ruleID, String commID, String feeTypeID, String ruleType,
			String roundType, String periodType, String status,
			String validityType) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", ruleID);
		map.put("commID", commID);
		map.put("feeTypeID", feeTypeID);
		map.put("ruleType", ruleType);
		map.put("roundType", roundType);
		map.put("periodType", periodType);
		map.put("status", status);
		map.put("validityType", validityType);

		List<PropFeeComputeRuleDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleInfoSearchMapper.findPropFeeComputeRuleInfoDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeComputeRuleDto> bauthfindPropFeeComputeRuleInfo(
			List<String> propFeeComputeRuleInfoIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeComputeRuleDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleInfoSearchMapper.batchFindPropFeeComputeRuleInfo",
						propFeeComputeRuleInfoIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeComputeRuleInfoByMap(
			Map<String, Object> propFeeComputeRuleInfo) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeComputeRuleInfoSearchMapper.batchFindPropFeeComputeRuleInfoByMap",
						propFeeComputeRuleInfo);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editFeeComputeRule(PropFeeComputeRule propFeeComputeRule)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeComputeRule.getCommID())) {
			throw new AppBizException(2, "亲，小区信息");
		}

		if (StringUtils.isBlank(propFeeComputeRule.getRuleID())) {
			throw new AppBizException(2, "亲，规则信息");
		}

		if (StringUtils.isBlank(propFeeComputeRule.getRuleName())) {
			throw new AppBizException(2, "亲，规则名称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getFeeTypeID())) {
			throw new AppBizException(2, "亲，收费类目称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleType())) {
			throw new AppBizException(2, "亲，规则类型");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRule())) {
			throw new AppBizException(2, "亲，规则信息");
		}
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() == null
				|| propFeeComputeRule.getPropFeeRuleLinkApartments().size() <= 0) {
			throw new AppBizException(2, "亲，房产关联信息");
		}

		/***
		 * 生成周期(1按月，2按季)
		 */
		if (StringUtils.isBlank(propFeeComputeRule.getPeriodType())) {
			String periodVal = propFeeCommInfoService.findValByCode(
					propFeeComputeRule.getCommID(), "period");
			if (StringUtils.isBlank(periodVal)) {
				throw new AppBizException(2, "亲，请先小区配置缴费周期");
			}
			propFeeComputeRule.setPeriodType(periodVal);
		}
		if (StringUtils.isBlank(propFeeComputeRule.getStatus())) {
			propFeeComputeRule.setStatus("1");
		}
		List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = propFeeComputeRuleService
				.findPropFeeComputeRuleByRelevantID(
						propFeeComputeRule.getRuleName(), null,
						propFeeComputeRule.getCommID(),
						propFeeComputeRule.getFeeTypeID(), null, null, null,
						null);
		if (propFeeComputeRuleDtos != null && propFeeComputeRuleDtos.size() > 1) {
			throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");
		}

		if (propFeeComputeRuleDtos != null
				&& propFeeComputeRuleDtos.size() == 1) {
			if (!propFeeComputeRuleDtos.get(0).getRuleID()
					.equals(propFeeComputeRule.getRuleID())) {
				throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");
			}
		}

		/***
		 * 添加删除新增日志
		 */
		PropFeeComputeRuleDto oldPropFeeComputeRule = propFeeComputeRuleService
				.findPropFeeComputeRuleByID(propFeeComputeRule.getRuleID());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", oldPropFeeComputeRule.getRuleID());
		map.put("invalid", "0");
		oldPropFeeComputeRule.setCreateuser(propFeeComputeRule.getCreateUser());
		PropFeeComputeRuleDto tempComputeRuleDto = new PropFeeComputeRuleDto();
		tempComputeRuleDto = oldPropFeeComputeRule.clone();
		this.addPropFeeComputeRuleInfo(tempComputeRuleDto);
		oldPropFeeComputeRule.setEdituserID(propFeeComputeRule
				.getCreateuserID());
		
		propFeeComputeRule.setInitID(oldPropFeeComputeRule.getInitID());
		propFeeComputeRuleService
				.deletePropFeeComputeRule(oldPropFeeComputeRule);

		/***
		 * 15:50:57 张统玮 2016/8/25 15:50:57 新增一个方法 在修改规则后 删除 ruleID 为该规则ID type=2
		 * 的缴费细项，prop_fee_detail
		 * 
		 * 张统玮 2016/8/25 15:51:27 修改 或删除后有涉及 删除旧规则时
		 * 
		 * 张统玮 2016/8/25 15:51:36 未支付
		 */
		this.getSqlSession().update(
				"PropFeeDetailBauthMapper.deleteNoPayDetail", map);
		/****
		 * 新增新的规则
		 */
		propFeeComputeRule.setRuleID(IdUtil.getuuId());
		PropFeeTypeDto propFeeTypeDto = propFeeTypeService
				.findPropFeeTypeByID(propFeeComputeRule.getFeeTypeID());
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() != null
				&& propFeeComputeRule.getPropFeeRuleLinkApartments().size() > 0) {
			List<String> apartmentIDs = new ArrayList<String>();
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {
				apartmentIDs.add(propFeeRuleLinkApartment.getApartmentID());
			}
			if (1==1||!propFeeTypeDto.getType().equals("3")
					|| (propFeeTypeDto.getType().equals("3") && propFeeComputeRule
							.getValidityType().equals("2"))) {
				List<Map<String, Object>> apartments = propFeeRuleLinkApartmentService
						.findRuleHouseInfo(null, null,
								propFeeComputeRule.getCommID(),
								propFeeComputeRule.getFeeTypeID(), null,
								apartmentIDs,
								propFeeComputeRule.getValidityType(),propFeeTypeDto.getType(),propFeeComputeRule.getRuleType(),
								propFeeComputeRule.getStartTime(),
								propFeeComputeRule.getEndTime());

				if (apartments != null && apartments.size() > 0) {
					StringBuffer buffer = new StringBuffer();
					for (Map<String, Object> apart : apartments) {
						if (apart.get("unitName") != null) {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("unitName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");
						} else {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");

						}

					}
					throw new AppBizException(2, "亲，" + buffer.toString());
				}
			}

			/***
			 * 添加业主的产权关联
			 */
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {

				/***
				 * 0固定, 1公摊类，2计量类，3临时类'
				 */

				propFeeRuleLinkApartment.setCommID(propFeeComputeRule
						.getCommID());
				propFeeRuleLinkApartment.setRuleID(propFeeComputeRule
						.getRuleID());
				propFeeRuleLinkApartment.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkApartment.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkApartment.setTscreate(new Date());
				propFeeRuleLinkApartment.setTsedit(new Date());
				propFeeRuleLinkApartment.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
			}

			this.getSqlSession().update(
					"PropFeeRuleLinkApartmentBauthMapper.bauthDeleteByRuleIDs",
					map);
			this.getSqlSession().insert(
					"PropFeeRuleLinkApartmentBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkApartments());
		}

		/***
		 * 添加参数
		 */
		if (propFeeComputeRule.getPropFeeRuleParams() != null
				&& propFeeComputeRule.getPropFeeRuleParams().size() > 0) {
			for (PropFeeRuleParamDto propFeeRuleParam : propFeeComputeRule
					.getPropFeeRuleParams()) {
				propFeeRuleParam.setParamID(IdUtil.getuuId());
				propFeeRuleParam.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleParam.setTscreate(new Date());
				propFeeRuleParam.setTsedit(new Date());
				propFeeRuleParam.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
				propFeeRuleParam.setRuleID(propFeeComputeRule.getRuleID());
				if (StringUtils.isBlank(propFeeRuleParam.getParamType())) {
					propFeeRuleParam.setParamType("1");
				}
			}
			this.getSqlSession().update(
					"PropFeeRuleParamBauthMapper.bauthDeleteByRuleIDs", map);
			this.getSqlSession().insert(
					"PropFeeRuleParamBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleParams());
		}

		/***
		 * 添加参数
		 */

		return propFeeComputeRuleService
				.addPropFeeComputeRule(propFeeComputeRule);

	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPoolTypeFeeComputeRule(PropFeeComputeRule propFeeComputeRule)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeComputeRule.getCommID())) {
			throw new AppBizException(2, "亲，小区信息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleID())) {
			throw new AppBizException(2, "亲，规则信息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleName())) {
			throw new AppBizException(2, "亲，规则名称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getFeeTypeID())) {
			throw new AppBizException(2, "亲，收费类目称息");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRuleType())) {
			throw new AppBizException(2, "亲，规则类型");
		}
		if (StringUtils.isBlank(propFeeComputeRule.getRule())) {
			throw new AppBizException(2, "亲，规则信息");
		}
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() == null
				|| propFeeComputeRule.getPropFeeRuleLinkApartments().size() <= 0) {
			throw new AppBizException(2, "亲，房产关联信息");
		}
		/***
		 * 生成周期(1按月，2按季)
		 */
		if (StringUtils.isBlank(propFeeComputeRule.getPeriodType())) {
			String periodVal = propFeeCommInfoService.findValByCode(
					propFeeComputeRule.getCommID(), "period");
			if (StringUtils.isBlank(periodVal)) {
				throw new AppBizException(2, "亲，请先小区配置缴费周期");
			}
			propFeeComputeRule.setPeriodType(periodVal);
		}

		if (StringUtils.isBlank(propFeeComputeRule.getStatus())) {
			propFeeComputeRule.setStatus("1");
		}
		/***
		 * 判断房屋关联
		 */

		List<PropFeeComputeRuleDto> propFeeComputeRuleDtos = propFeeComputeRuleService
				.findPropFeeComputeRuleByRelevantID(
						propFeeComputeRule.getRuleName(), null,
						propFeeComputeRule.getCommID(),
						propFeeComputeRule.getFeeTypeID(), null, null, null,
						null);

		if (propFeeComputeRuleDtos != null && propFeeComputeRuleDtos.size() > 1) {
			throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");

		}
		if (propFeeComputeRuleDtos != null
				&& propFeeComputeRuleDtos.size() == 1) {
			if (!propFeeComputeRuleDtos.get(0).getRuleID()
					.equals(propFeeComputeRule.getRuleID())) {
				throw new AppBizException(2, "亲，该费用类目的规则名称已经存在");
			}
		}

		/***
		 * 添加删除新增日志
		 */
		PropFeeComputeRuleDto oldPropFeeComputeRule = propFeeComputeRuleService
				.findPropFeeComputeRuleByID(propFeeComputeRule.getRuleID());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", oldPropFeeComputeRule.getRuleID());
		map.put("invalid", "0");
		oldPropFeeComputeRule.setCreateuser(propFeeComputeRule.getCreateUser());
		PropFeeComputeRuleDto tempComputeRuleDto = new PropFeeComputeRuleDto();
		tempComputeRuleDto = oldPropFeeComputeRule.clone();
		this.addPropFeeComputeRuleInfo(tempComputeRuleDto);
		oldPropFeeComputeRule.setEdituserID(propFeeComputeRule
				.getCreateuserID());
		
		propFeeComputeRule.setInitID(oldPropFeeComputeRule.getInitID());
		propFeeComputeRuleService
				.deletePropFeeComputeRule(oldPropFeeComputeRule);

		/***
		 * 15:50:57 张统玮 2016/8/25 15:50:57 新增一个方法 在修改规则后 删除 ruleID 为该规则ID type=2
		 * 的缴费细项，prop_fee_detail
		 * 
		 * 张统玮 2016/8/25 15:51:27 修改 或删除后有涉及 删除旧规则时
		 * 
		 * 张统玮 2016/8/25 15:51:36 未支付
		 */
		this.getSqlSession().update(
				"PropFeeDetailBauthMapper.deleteNoPayDetail", map);

		/****
		 * 新增新的规则
		 */
		PropFeeTypeDto propFeeTypeDto = propFeeTypeService
				.findPropFeeTypeByID(propFeeComputeRule.getFeeTypeID());
		propFeeComputeRule.setRuleID(IdUtil.getuuId());
		/***
		 * 添加房屋关联
		 */
		if (propFeeComputeRule.getPropFeeRuleLinkApartments() != null
				&& propFeeComputeRule.getPropFeeRuleLinkApartments().size() > 0) {

			List<String> apartmentIDs = new ArrayList<String>();
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {
				apartmentIDs.add(propFeeRuleLinkApartment.getApartmentID());
			}
			if (1==1||!propFeeTypeDto.getType().equals("3")
					|| (propFeeTypeDto.getType().equals("3") && propFeeComputeRule
							.getValidityType().equals("2"))) {
				List<Map<String, Object>> apartments = propFeeRuleLinkApartmentService
						.findRuleHouseInfo(null, null,
								propFeeComputeRule.getCommID(),
								propFeeComputeRule.getFeeTypeID(), null,
								apartmentIDs,
								propFeeComputeRule.getValidityType(),propFeeTypeDto.getType(),propFeeComputeRule.getRuleType(),
								propFeeComputeRule.getStartTime(),
								propFeeComputeRule.getEndTime());
				if (apartments != null && apartments.size() > 0) {
					StringBuffer buffer = new StringBuffer();
					for (Map<String, Object> apart : apartments) {
						if (apart.get("unitName") != null) {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("unitName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");
						} else {
							buffer.append("房屋")
									.append(apart.get("buildingName"))
									.append(apart.get("apartmentNum"))
									.append("已经被其他规则(" + apart.get("ruleName")
											+ ")使用").append("\t\t\r\n")
									.append(",");

						}

					}
					throw new AppBizException(2, "亲，" + buffer.toString());
				}
			}

			/***
			 * 添加业主的产权关联
			 */
			for (PropFeeRuleLinkApartmentDto propFeeRuleLinkApartment : propFeeComputeRule
					.getPropFeeRuleLinkApartments()) {
				propFeeRuleLinkApartment.setCommID(propFeeComputeRule
						.getCommID());
				propFeeRuleLinkApartment.setRuleID(propFeeComputeRule
						.getRuleID());
				propFeeRuleLinkApartment.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkApartment.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkApartment.setTscreate(new Date());
				propFeeRuleLinkApartment.setTsedit(new Date());
				propFeeRuleLinkApartment.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
			}
			this.getSqlSession().update(
					"PropFeeRuleLinkApartmentBauthMapper.bauthDeleteByRuleIDs",
					map);
			this.getSqlSession().insert(
					"PropFeeRuleLinkApartmentBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkApartments());
		}

		/****
		 * 添加公摊关联
		 */
		if (propFeeComputeRule.getPropFeeRuleLinkMeter() != null
				&& propFeeComputeRule.getPropFeeRuleLinkMeter().size() > 0) {
			for (PropFeeRuleLinkMeterDto propFeeRuleLinkMeter : propFeeComputeRule
					.getPropFeeRuleLinkMeter()) {
				propFeeRuleLinkMeter.setCommID(propFeeComputeRule.getCommID());
				propFeeRuleLinkMeter.setRuleID(propFeeComputeRule.getRuleID());
				propFeeRuleLinkMeter.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
				propFeeRuleLinkMeter.setLinkID(IdUtil.getuuId());
				propFeeRuleLinkMeter.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleLinkMeter.setTscreate(new Date());
				propFeeRuleLinkMeter.setTsedit(new Date());
			}
			this.getSqlSession()
					.update("PropFeeRuleLinkMeterBauthMapper.bauthDeleteByRuleIDs",
							map);

			this.getSqlSession().insert(
					"PropFeeRuleLinkMeterBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleLinkMeter());
		}

		/***
		 * 添加参数
		 */
		if (propFeeComputeRule.getPropFeeRuleParams() != null
				&& propFeeComputeRule.getPropFeeRuleParams().size() > 0) {
			for (PropFeeRuleParamDto propFeeRuleParam : propFeeComputeRule
					.getPropFeeRuleParams()) {
				propFeeRuleParam.setParamID(IdUtil.getuuId());
				propFeeRuleParam.setItemStat(TypeConst.FLAG_ON);
				propFeeRuleParam.setTscreate(new Date());
				propFeeRuleParam.setTsedit(new Date());
				propFeeRuleParam.setCreateuserID(propFeeComputeRule
						.getCreateuserID());
				propFeeRuleParam.setRuleID(propFeeComputeRule.getRuleID());
				if (StringUtils.isBlank(propFeeRuleParam.getParamType())) {
					propFeeRuleParam.setParamType("1");
				}
			}
			this.getSqlSession().update(
					"PropFeeRuleParamBauthMapper.bauthDeleteByRuleIDs", map);
			this.getSqlSession().insert(
					"PropFeeRuleParamBauthMapper.bauthInsert",
					propFeeComputeRule.getPropFeeRuleParams());
		}
		/***
		 * 添加参数
		 */
		return propFeeComputeRuleService
				.addPropFeeComputeRule(propFeeComputeRule);

	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeComputeRuleByLogic(String propFeeComputeRuleID,
			String userName, String userID) throws AppBizException {
		if (StringUtils.isBlank(propFeeComputeRuleID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeComputeRuleDto oldPropFeeComputeRule = propFeeComputeRuleService
				.findPropFeeComputeRuleByID(propFeeComputeRuleID);

		/***
		 * 删除关联连接
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleID", propFeeComputeRuleID);
		map.put("invalid", "0");
		map.put("edituserID", userID);

		this.getSqlSession()
				.update("PropFeeRuleLinkApartmentBauthMapper.bauthDeleteByRuleIDs",
						map);
		this.getSqlSession().update(
				"PropFeeRuleLinkMeterBauthMapper.bauthDeleteByRuleIDs", map);
		this.getSqlSession().update(
				"PropFeeRuleParamBauthMapper.bauthDeleteByRuleIDs", map);

		/***
		 * 15:50:57 张统玮 2016/8/25 15:50:57 新增一个方法 在修改规则后 删除 ruleID 为该规则ID type=2
		 * 的缴费细项，prop_fee_detail
		 * 
		 * 张统玮 2016/8/25 15:51:27 修改 或删除后有涉及 删除旧规则时
		 * 
		 * 张统玮 2016/8/25 15:51:36 未支付
		 */
		this.getSqlSession().update(
				"PropFeeDetailBauthMapper.deleteNoPayDetail", map);
		oldPropFeeComputeRule.setTsedit(new Date());
		oldPropFeeComputeRule.setCreateuser(userName);
		PropFeeComputeRuleDto tempComputeRuleDto = new PropFeeComputeRuleDto();
		tempComputeRuleDto = oldPropFeeComputeRule.clone();
		this.addPropFeeComputeRuleInfo(tempComputeRuleDto);
		oldPropFeeComputeRule.setItemStat(TypeConst.FLAG_OFF);
		return this.getSqlSession().update(
				"PropFeeComputeRuleMapper.updateByPrimaryKeySelective",
				oldPropFeeComputeRule);
	}

}